package coml.jiyun.hp.day04;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CircleProgressBar extends View {
    String TAG="CircleProgressBar";

    private  int ring_color;
    private  float ring_width;
    private  int circle_color;
    private  float start_radiu;
    private  float sweep_radiu;
    private  String text;
    private  float text_size;
    private  int text_color;
    private final Paint ring_paint;
    private RectF rectF;
    private int x;
    private int y;
    private int radius;
    private final Paint circle_paint;
    private final Paint text_paint;
    private final float dy;

    //xml在转换成java代码的时候会将所有的属性，封装到AttributeSet对象中
    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        if(array!=null){
            //第一个参数，属性再arrtrs.xml中的名字
            //第二个参数，默认值
            ring_color = array.getColor(R.styleable.CircleProgressBar_ring_color, 0);
            ring_width = array.getDimension(R.styleable.CircleProgressBar_ring_width, 20);
            circle_color = array.getColor(R.styleable.CircleProgressBar_circle_color, 0);
            start_radiu = array.getFloat(R.styleable.CircleProgressBar_start_radiu, 0);
            sweep_radiu = array.getFloat(R.styleable.CircleProgressBar_sweep_radiu, 0);
            text = array.getString(R.styleable.CircleProgressBar_android_text);
            text_size = array.getDimension(R.styleable.CircleProgressBar_android_textSize, 30);
            text_color = array.getColor(R.styleable.CircleProgressBar_android_textColor, 0);

            Log.d(TAG, "ring_width: "+ring_width);
            //回收资源
            array.recycle();
        }


        //环的画笔
        ring_paint = new Paint();
        //去锯齿
        ring_paint.setAntiAlias(true);
        ring_paint.setStrokeWidth(ring_width);
        ring_paint.setColor(ring_color);
        ring_paint.setStyle(Paint.Style.STROKE);


        //圆的画笔
        circle_paint = new Paint();
        circle_paint.setAntiAlias(true);
        circle_paint.setColor(circle_color);

        //文字画笔
        text_paint = new Paint();
        text_paint.setColor(text_color);
        text_paint.setTextSize(text_size);

        //水平居中
        text_paint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = text_paint.getFontMetrics();
        //dy = 文本高度的1半 - descentline到baseline的距离 =
        //（decent - ascent）*1/2 -descent
        dy = (float) ((fontMetrics.descent - fontMetrics.ascent) * 0.5 - fontMetrics.descent);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        //为了避免在使用的时候给定的控件不是正方形，我们可以进行纠正
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int max = Math.max(width, height);
        //生成一个宽或者高的期望
        int spec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY);
        super.onMeasure(spec, spec);

        rectF = new RectF(0.1f * max, 0.1f * max, 0.9f * max, 0.9f * max);
        //圆心
        x = max / 2;
        y = max / 2;
        //半径
        radius = max / 4;
    }

    //在绘制的方法onDraw() 绘制想要的内容

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画环
        canvas.drawArc(rectF,start_radiu,sweep_radiu,false,ring_paint);

        //画圆
        canvas.drawCircle(x,y,radius,circle_paint);

        //画文本
        canvas.drawText(text,x,y+dy,text_paint);
    }

    public void setProgress(int progress){
        text=progress+"%";
        sweep_radiu=progress*3.6f;
        //j刷新界面
        //只能在ui线程使用
        postInvalidate();
    }
}
